﻿using UnityEngine;

namespace FwtUnityEngine
{
    public class Sprite2D : FwtMonoBehaviour
    {
        public MeshFilter MeshFilter;
        public MeshRenderer MeshRenderer;

        public Vector2 Size
        {
            get { return transform.localScale; }
            set { transform.localScale = value; }
        }

        private Material _texture;
        public Material Texture
        {
            get { return _texture; }
            set
            {
                _texture = value;
                MeshRenderer.sharedMaterial = value;
            }
        }

        private Rect _textureCoordinates;
        public Rect TextureCoordinates
        {
            get { return _textureCoordinates; }
            set
            {
                _textureCoordinates = value;
                Mesh = GeneralFactory.ChangeTextureCoords(Mesh, value);
            }
        }

        public Mesh Mesh
        {
            get { return MeshFilter.mesh; }
            set { MeshFilter.mesh = value; }
        }

        public void Init()
        {
            MeshFilter = GetComponent(typeof(MeshFilter)) as MeshFilter;
            MeshRenderer = GetComponent(typeof(MeshRenderer)) as MeshRenderer;

            if (MeshFilter != null)
                MeshFilter.mesh = GeneralFactory.CreateSquareMesh();
        }
    }
}
